package cn.stylefeng.guns.modular.business.frequency.transportleveldetail.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.modular.business.frequency.transportleveldetail.entity.TransportLevelDetail;
import cn.stylefeng.guns.modular.business.frequency.transportleveldetail.mapper.TransportLevelDetailMapper;
import cn.stylefeng.guns.modular.business.frequency.transportleveldetail.model.param.TransportLevelDetailParam;
import cn.stylefeng.guns.modular.business.frequency.transportleveldetail.service.TransportLevelDetailService;
import cn.stylefeng.guns.modular.business.frequency.transportleveldetail.enums.TransportLevelDetailExceptionEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 运单目的地以及费用记录 服务实现类
 *
 * @author yujie
 * @date 2023/05/09 17:15
 */
@Service
public class TransportLevelDetailServiceImpl extends ServiceImpl<TransportLevelDetailMapper, TransportLevelDetail> implements TransportLevelDetailService {

    @Override
    public PageResult<TransportLevelDetail> page(TransportLevelDetailParam transportLevelDetailParam) {

        // 构造条件
        LambdaQueryWrapper<TransportLevelDetail> queryWrapper = new LambdaQueryWrapper<>();

        // 查询分页结果
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<TransportLevelDetail> list(TransportLevelDetailParam transportLevelDetailParam) {

        // 构造条件
        LambdaQueryWrapper<TransportLevelDetail> queryWrapper = new LambdaQueryWrapper<>();

        return this.list(queryWrapper);
    }

    @Override
    public void add(TransportLevelDetailParam transportLevelDetailParam) {

        // 将dto转为实体
        TransportLevelDetail transportLevelDetail = new TransportLevelDetail();
        BeanUtil.copyProperties(transportLevelDetailParam, transportLevelDetail);

        this.save(transportLevelDetail);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(TransportLevelDetailParam transportLevelDetailParam) {
        this.removeById(transportLevelDetailParam.getId());
    }

    @Override
    public void edit(TransportLevelDetailParam transportLevelDetailParam) {

        // 根据id查询实体
        TransportLevelDetail transportLevelDetail = this.queryTransportLevelDetail(transportLevelDetailParam);

        // 请求参数转化为实体
        BeanUtil.copyProperties(transportLevelDetailParam, transportLevelDetail);

        this.updateById(transportLevelDetail);
    }

    @Override
    public TransportLevelDetail detail(TransportLevelDetailParam transportLevelDetailParam) {
        return this.queryTransportLevelDetail(transportLevelDetailParam);
    }



    /**
     * 获取运单目的地以及费用记录
     *
     * @author yujie
     * @since 2023/05/09 17:15
     */
    private TransportLevelDetail queryTransportLevelDetail(TransportLevelDetailParam transportLevelDetailParam) {
        TransportLevelDetail transportLevelDetail = this.getById(transportLevelDetailParam.getId());
        if (ObjectUtil.isEmpty(transportLevelDetail)) {
            throw new ServiceException(TransportLevelDetailExceptionEnum.NOT_EXISTED);
        }
        return transportLevelDetail;
    }
}